package com.ruoyi.system.basicInformation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.ruoyi.system.basicInformation.bo.TChangeShiftsAddBo;
import com.ruoyi.system.basicInformation.bo.TChangeShiftsQueryBo;
import com.ruoyi.system.basicInformation.bo.TChangeShiftsEditBo;
import com.ruoyi.system.basicInformation.domain.TChangeShifts;
import com.ruoyi.system.basicInformation.mapper.TChangeShiftsMapper;
import com.ruoyi.system.basicInformation.vo.TChangeShiftsVo;
import com.ruoyi.system.basicInformation.service.ITChangeShiftsService;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 交接班记录Service业务层处理
 *
 * @author ruoyi
 * @date 2021-05-19
 */
@Service
public class TChangeShiftsServiceImpl extends ServiceImpl<TChangeShiftsMapper, TChangeShifts> implements ITChangeShiftsService {

    @Override
    public TChangeShiftsVo queryById(Long id){
        TChangeShiftsVo db = this.baseMapper.selectById(id);
        return BeanUtil.toBean(db, TChangeShiftsVo.class);
    }

    @Override
    public List<TChangeShiftsVo> queryList(TChangeShiftsQueryBo bo) {
//        LambdaQueryWrapper<TChangeShifts> lqw = Wrappers.lambdaQuery();
//        lqw.like(StrUtil.isNotBlank(bo.getContent()), TChangeShifts::getContent, bo.getContent());
        List<TChangeShiftsVo> lqw = this.baseMapper.selectDTO(bo);
        return lqw;
    }

    /**
    * 实体类转化成视图对象
    *
    * @param collection 实体类集合
    * @return
    */
    private List<TChangeShiftsVo> entity2Vo(Collection<TChangeShifts> collection) {
        List<TChangeShiftsVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, TChangeShiftsVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<TChangeShifts> page = (Page<TChangeShifts>)collection;
            Page<TChangeShiftsVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page,pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }

    @Override
    public Boolean insertByAddBo(TChangeShiftsAddBo bo) {
        TChangeShifts add = BeanUtil.toBean(bo, TChangeShifts.class);
        validEntityBeforeSave(add);
        return this.save(add);
    }

    @Override
    public Boolean updateByEditBo(TChangeShiftsEditBo bo) {
        TChangeShifts update = BeanUtil.toBean(bo, TChangeShifts.class);
        validEntityBeforeSave(update);
        return this.updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(TChangeShifts entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return this.removeByIds(ids);
    }
}
